package com.zxy.controller;

import com.zxy.config.annotation.Doc;
import com.zxy.config.annotation.NeedAuthorization;
import com.zxy.config.jwt.JwtUtil;
import com.zxy.domain.User;
import com.zxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JwtUtil jwtUtil;

    @Resource
    private UserService userService;

    @Doc(doc = "用户登录接口")
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public Map<String,Object> login(String email,String password){
        User user = userService.findUserByEmail(email, password);
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("email",user.getEmail());
        String token = jwtUtil.createToken(claims);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);
        return map;
    }

    @Doc(doc = "首页权限测试接口")
    @NeedAuthorization(role = "admin")
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "首页";
    }
}
